#!/usr/bin/env bash

# ============================================================================
# -- Choose your desired compiler --------------------------------------------
# ============================================================================
COMP=gcc
MAKE_JOBCOUNT=8
PYTHON_VERSION=3.8.2

# ============================================================================
# -- Do not modify below this line -------------------------------------------
# ============================================================================
function err() {
    >&2 echo -e "\033[0;31m[ERROR]\033[0m" $@
    return 0
}

function info() {
    echo -e "\033[0;34m[INFO]\033[0m" $@
}

if [ "${BASH_SOURCE-}" != "$0" ]; then
    err "Script is being sourced. Please run 'install' in a subshell instead"
    return 0
fi

CWD=${PWD}
trap "cd ${CWD}" EXIT
trap "cd ${CWD}" SIGINT

cd "$( dirname "${BASH_SOURCE[0]}" )"/..    # move to PRGA_ROOT

# find git
GIT=$( command -v git 2>/dev/null )
if [ -z "${GIT}" ]; then
    err "'git' not found" || exit 1
fi
info "Using 'git': ${GIT}"

# find make
MAKE=$( command -v make 2>/dev/null )
if [ -z "${MAKE}" ]; then
    err "'make' not found" || exit 1
fi
info "Using 'make': ${MAKE}"

# find cmake
CMAKE=$( command -v cmake3 2>/dev/null )
if [ -z "${CMAKE}" ]; then
    CMAKE=$( command -v cmake 2>/dev/null )
    if [ -z "${CMAKE}" ]; then
        err "'cmake' not found" || exit 1
        exit 1
    elif [[ $( ${CMAKE} --version | cut -d " " -f 3 ) != "3"* ]]; then
        err "CMake 3.x is required to build VPR" || exit 1
    fi
fi
info "Using 'cmake': ${CMAKE}"

# find C compiler
CC=$( command -v ${COMP} 2>/dev/null )
if [ -z "${CC}" ]; then
    err "'${COMP}' not found" || exit 1
fi
info "Using '${COMP}': ${CC}"

# find or install pyenv
if command -v pyenv 2>&1 >/dev/null; then
    info "Active 'pyenv' found. Skipping installation/activation of 'pyenv'"
else
    if [ ! -f pyenv/bin/pyenv ]; then
        info "'pyenv' not found. Installing 'pyenv' locally to ${PWD}/pyenv"
        ${GIT} clone https://github.com/pyenv/pyenv.git || exit 1
        info "'pyenv' installed locally to ${PWD}/pyenv"
    else
        info "Locally-installed 'pyenv' found. Updating 'pyenv'"
        cd pyenv && ${GIT} fetch && ${GIT} pull && cd ..
        info "'pyenv' updated"
    fi

    info "Activating locally-installed 'pyenv'"
    export PYENV_ROOT=${PWD}/pyenv
    export PATH=${PYENV_ROOT}/bin:$PATH
    eval "$( pyenv init - )"
    info "'pyenv' activated"
fi

# install the desired python if it's not installed already
info "Installing Python ${PYTHON_VERSION} with 'pyenv'"
env PYTHON_CONFIGURE_OPTS="--enable-shared" CFLAGS="-fPIC" CXXFLAGS="-fPIC" pyenv install -s ${PYTHON_VERSION} || exit 1
info "Python ${PYTHON_VERSION} installed with 'pyenv'"
pyenv local ${PYTHON_VERSION} || exit 1
if [[ $( python --version | cut -d " " -f 2 ) != "${PYTHON_VERSION}" ]]; then
    err "'pyenv' is not choosing the correct Python version" || exit 1
fi
info "Python ${PYTHON_VERSION} chosen as the local version"

# upgrade pip and setuptools
info "Upgrading 'pip' and 'setuptools'"
pip install --upgrade setuptools pip
info "'pip' and 'setuptools' upgraded"

# install pipenv
info "Installing 'pipenv'"
pip install --upgrade pipenv || exit 1
info "'pipenv' installed"

# check out submodules
info "Checking out submodules"
${GIT} submodule update --init --recursive || exit 1
info "Submodules checked out"

mkdir -p local

# build VTR
info "Building VTR"
cd vtr
${MAKE} -j${MAKE_JOBCOUNT} CMAKE=${CMAKE} || exit 1
cd ..
info "VTR built successfully"

# build yosys
info "Building Yosys"
${MAKE} -C yosys -j${MAKE_JOBCOUNT} CONFIG=${COMP} PREFIX=${PWD}/local || exit 1
${MAKE} -C yosys install CONFIG=${COMP} PREFIX=${PWD}/local || exit 1
info "Yosys built successfully"

# link all binaries
info "Linking binaries to ${PWD}/local/bin"
cd local/bin
cp ../../vtr/vpr/vpr vpr
cp ../../vtr/build/utils/fasm/genfasm genfasm
cd ../..
info "'vpr' and 'genfasm' linked to ${PWD}/local/bin"

# build a virtualenv for prga.py
cd prga.py
info "Creating a virtualenv for prga.py"
PIPENV_VENV_IN_PROJECT=1 pipenv --python ${PYTHON_VERSION} install -e .[cocotb] || exit 1
cd ..
info "Virtualenv setup completed for prga.py"

info "Installation finished successfully"

# vim: set ft=sh:
